package cn.hxth.cloud.checkprovider.service.impl;

import cn.hxth.cloud.api.common.constant.ProcedureStatus;
import cn.hxth.cloud.api.common.util.AbstractBaseServiceImpl;
import cn.hxth.cloud.api.common.util.DateProvider;
import cn.hxth.cloud.api.domain.*;
import cn.hxth.cloud.api.mapper.CheckOrderMapper;
import cn.hxth.cloud.api.mapper.ProductMapper;
import cn.hxth.cloud.api.rabbitmq.RabbitMQSender;
import cn.hxth.cloud.checkprovider.service.CheckService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class CheckServiceImpl extends AbstractBaseServiceImpl implements CheckService {
    @Autowired
    private CheckOrderMapper checkOrderMapper;

    private Logger logger = LoggerFactory.getLogger(CheckServiceImpl.class);
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    public CheckServiceImpl(RabbitMQSender rabbitMQSender, RedisTemplate<String, String> redisTemplate, DateProvider dateProvider) {
        super(rabbitMQSender, redisTemplate, dateProvider);
    }

    @Override
    public Boolean insertCheckOrder(ProductionOrder productionOrder) {
        try {
            Map<Long, ProductInputOrderItem> productInputOrderItemMap = null;
            long count = 0;
            Product product = productMapper.selectByPrimaryKey(productionOrder.getPid());
            Double maxDiameter = product.getMaxDiameter();
            Double minDiameter = product.getMinDiameter();
            Double maxHeight = product.getMaxHeight();
            Double minHeight = product.getMinHeight();
            Double maxInradium = product.getMaxInradium();
            Double minInradium = product.getMinInradium();
            Double maxThickness = product.getMaxThickness();
            Double minThickness = product.getMinThickness();
            for (Turnoff turnoff : productionOrder.getTurnoffs()) {
                if (
                                turnoff.getDiameter() <= maxDiameter && turnoff.getDiameter() >= minDiameter &&
                                turnoff.getHeight() <= maxHeight && turnoff.getHeight() >= minHeight &&
                                turnoff.getInradium() <= maxInradium && turnoff.getInradium() >= minInradium &&
                                turnoff.getThickness() <= maxThickness && turnoff.getThickness() >= minThickness


                        )
                    count++;
            }
            CheckOrder checkOrder = new CheckOrder();
            checkOrder.setMakeDate(dateProvider.getCurrentTime());
            checkOrder.setAcceptanceNumber(count);
            checkOrder.setNeedCount(productionOrder.getNeedCount());
            checkOrder.setPid(productionOrder.getId());
            checkOrder.setCheckCount(productionOrder.getPlanCount());
            this.checkOrderMapper.insertSelective(checkOrder);
            if (!(checkOrder.getAcceptanceNumber() >= checkOrder.getNeedCount())) {
                ProductionOrder newProductionOrder = new ProductionOrder();
                productionOrder.clone(newProductionOrder);
                // newProductionOrder.setDeliveryDate(productionOrder.getDeliveryDate());
                newProductionOrder.setProcedureStatus(ProcedureStatus.STOCKING);
                newProductionOrder.setPlanCount(checkOrder.getNeedCount() - checkOrder.getAcceptanceNumber() + 1000);
                //newProductionOrder.setProduct(product);
                //newProductionOrder.setOid(productionOrder.getOid());
                //newProductionOrder.setPid(productionOrder.getPid());
                newProductionOrder.setSteelsCount(1000l);
                newProductionOrder.setNeedCount(checkOrder.getNeedCount() - checkOrder.getAcceptanceNumber());
                newProductionOrder.setId(null);
                newProductionOrder.setMakeDate(dateProvider.getCurrentTime());
                super.sendMessage("production", newProductionOrder, "insertProductionOrder", null);
            }
            String jsonData = redisTemplate.opsForValue().get("productInputOrderItem");
            if (jsonData == null) {
                productInputOrderItemMap = new HashMap<>();

            } else {
                productInputOrderItemMap = JSONObject.parseObject(jsonData, new TypeReference<Map<Long, ProductInputOrderItem>>() {
                });
            }
            if (productInputOrderItemMap.containsKey(checkOrder.getPid())) {
                ProductInputOrderItem productInputOrderItem = productInputOrderItemMap.get(checkOrder.getPid());
                productInputOrderItem.setCount(productInputOrderItem.getCount() + count);
            } else {
                ProductInputOrderItem productInputOrderItem = new ProductInputOrderItem();
                productInputOrderItem.setPid(checkOrder.getPid());
                productInputOrderItem.setCount(count);
                productInputOrderItemMap.put(checkOrder.getPid(), productInputOrderItem);
                this.redisTemplate.opsForValue().set("productInputOrderItem", JSONObject.toJSONString(productInputOrderItemMap));

            }
        } catch (Exception e) {
            logger.error("error", e);
            return false;

        }
        return true;
    }
}
